package com.motorola.accessories;

import java.util.ArrayList;

import com.motorola.accessories.provider.ProviderInfo;

import android.content.AsyncQueryHandler;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class DeleteAccessoriesActivity extends PreferenceActivity {
    private static final String TAG = "MotoAccessoriesActivity";
    private static final int MESSAGE_QUERY_TOKEN = 1;

    private PreferenceScreen mPreferenceScreen = null;
    private final ArrayList<DeleteAccessoriesEntry> mDockList = new ArrayList<DeleteAccessoriesEntry>();
    private final ArrayList<DeleteAccessoriesEntry> mHeadsetList = new ArrayList<DeleteAccessoriesEntry>();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.accessories_delete);
        addPreferencesFromResource(R.xml.accessories_screen);
        mPreferenceScreen = getPreferenceScreen();
        Button cancel = (Button) findViewById(R.id.cancel);
        cancel.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                finish();
            }
        });
        Button delete = (Button) findViewById(R.id.delete);
        delete.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                deleteClick();
            }
        });
        startQuery();
    }

    private void startQuery(){
        QueryHandler queryHandler = new QueryHandler(this);
        try {
            queryHandler.startQuery(0, MESSAGE_QUERY_TOKEN, ProviderInfo.ACCESSORIES_CONTENT_URI, null, null, null, null);
        } catch (SQLiteException e) {
        }
    }

    private void deleteClick(){
        new AsyncTask<Void, Void, Boolean>() {
            protected Boolean doInBackground(Void... arg0) {
                boolean delete = false;
                for (int i = 0;i < mDockList.size();i++) {
                    if (mDockList.get(i).isChecked()) {
                        int id = mDockList.get(i).getId();
                        getContentResolver().delete(ProviderInfo.ACCESSORIES_CONTENT_URI, "_id=?", new String[]{String.valueOf(id)});
                        delete = true;
                    }
                }
                for (int i = 0;i < mHeadsetList.size();i++) {
                    if (mHeadsetList.get(i).isChecked()) {
                        int id = mHeadsetList.get(i).getId();
                        getContentResolver().delete(ProviderInfo.ACCESSORIES_CONTENT_URI, "_id=?", new String[]{String.valueOf(id)});
                        delete = true;
                    }
                }
                return delete;
            }
            protected void onPostExecute(Boolean needQuery){
                if (needQuery){
                    startQuery();
                }
            }
        }.execute();
    }

    private class QueryHandler extends AsyncQueryHandler {
        private Context mContext = null;

        public QueryHandler(Context context) {
            super(context.getContentResolver());
            mContext = context;
        }

        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
            mPreferenceScreen.removeAll();
            mDockList.clear();
            mHeadsetList.clear();
            if (cursor != null && cursor.moveToFirst() && cursor.getCount() != 0) {
                switch ((Integer) cookie) {
                    case MESSAGE_QUERY_TOKEN:
                        do {
                            int id = cursor.getInt(cursor.getColumnIndex(ProviderInfo.TableColumns.ID));
                            int category = cursor.getInt(cursor.getColumnIndex(ProviderInfo.TableColumns.ACCESSORY_CATEGORY));
                            String sw_version = cursor.getString(cursor.getColumnIndex(ProviderInfo.TableColumns.SW_VERSION));
                            switch (category) {
                            case 0:
                                mDockList.add(new DeleteAccessoriesEntry(mContext, sw_version, id));
                                break;
                            case 1:
                                mHeadsetList.add(new DeleteAccessoriesEntry(mContext, sw_version, id));
                                break;
                            default:
                                break;

                            }
                        } while (cursor.moveToNext());
                        break;
                    default:
                        Log.e(TAG, "Bad query token: " + token);
                        break;
                }

                cursor.close();
            }
            if (mDockList.size() + mHeadsetList.size() > 0) {
                mPreferenceScreen.addPreference(new TittlePreference(mContext, TittlePreference.HAS_INFO_TYPE));
                if (mDockList.size() > 0) {
                    mPreferenceScreen.addPreference(new TittlePreference(mContext, TittlePreference.DOCK_INFO_TYPE));
                    for (int i = 0;i < mDockList.size();i++) {
                        mPreferenceScreen.addPreference(mDockList.get(i));
                    }
                }
                if (mHeadsetList.size() > 0) {
                    mPreferenceScreen.addPreference(new TittlePreference(mContext, TittlePreference.HEARDSET_INFO_TYPE));
                    for (int i = 0;i < mHeadsetList.size();i++) {
                        mPreferenceScreen.addPreference(mHeadsetList.get(i));
                    }
                }
            } else {
                finish();
                Log.e(TAG, "There is not accessories need to delete.");
            }
            if (cursor != null) {
                cursor.close();
            }
        }
    }
}